#include "skinwindow.h"
#include "ui_skinwindow.h"
#include <QDesktopWidget>
#include <QPropertyAnimation>
#include <QParallelAnimationGroup>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QPainter>
#include <QLabel>
#include <QSlider>
#include <QScrollArea>
#include <QGraphicsDropShadowEffect>
#include "controls/titlebar.h"
#include "common.h"
#include "mainwindow.h"

const QString imageList[] = {
    "北极熊", "城市晚霞", "哆啦A梦", "奥运会", "晨曦", "麋鹿",
    "湖水", "灯塔", "狗狗", "立体图画", "蓝天", "蓝色山峰"
};

SkinWindow::SkinWindow(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SkinWindow)
{
    ui->setupUi(this);

    // 设置窗口透明
    this->setWindowOpacity(0);
    // 关闭就自我析构
    setAttribute(Qt::WA_DeleteOnClose);
    // 去边框
    setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint | windowFlags());
    setAttribute(Qt::WA_TranslucentBackground);
    setFixedSize(800, 590);

//    QGraphicsDropShadowEffect* effect = new QGraphicsDropShadowEffect(this);
//    effect->setBlurRadius(10);
//    effect->setColor(Qt::black);
//    effect->setOffset(0, 0);
//    this->setGraphicsEffect(effect);
    initSkinWindow();
}

void SkinWindow::initSkinWindow()
{
    // 创建标题栏对象
    TitleBar* pBar = new TitleBar(true);
    // 窗口标题
    pBar->setTitleName("360皮肤中心");
    // 设置拖动标题栏窗口跟随移动
    pBar->setMoveable(true);
    // 设置拖动标题栏的时候哪个窗口跟随移动
    pBar->setMainWindow(this);
    QVBoxLayout* vlayout = static_cast<QVBoxLayout*>(layout());
    // 标题栏插入到窗口布局中
    vlayout->insertWidget(0, pBar);

    connect(pBar, &TitleBar::minimumWin, this, &SkinWindow::showMinimized);
    connect(pBar, &TitleBar::closeWin, this, &SkinWindow::close);

    initSkinThumbnail();
}

void SkinWindow::showWgAnimation()
{
    show();
    QPropertyAnimation* pAnimation1 = new QPropertyAnimation(this, "geometry");
    QDesktopWidget* pDeskWg = QApplication::desktop();
    int x = (pDeskWg->availableGeometry().width() - width()) / 2;
    int y = (pDeskWg->availableGeometry().height() - height()) / 2;
    pAnimation1->setDuration(1000);  // 动画事件为1s
    pAnimation1->setStartValue(QRect(x, y+100, width(), height()));
    pAnimation1->setEndValue(QRect(x, y, width(), height()));
    pAnimation1->setEasingCurve(QEasingCurve::OutElastic);

    QPropertyAnimation *pAnimation2 = new QPropertyAnimation(this, "windowOpacity");
    pAnimation2->setDuration(1000);
    pAnimation2->setKeyValueAt(0.3, 0.5);
    pAnimation2->setKeyValueAt(1, 1);

    QParallelAnimationGroup *group = new QParallelAnimationGroup(this);
    group->addAnimation(pAnimation1);
    group->addAnimation(pAnimation2);
    group->start(QAbstractAnimation::DeleteWhenStopped);
}

void SkinWindow::initSkinThumbnail()
{
    // 网格布局
    QGridLayout* gridLayout = new QGridLayout;
    // 初始化皮肤图片列表
    m_mapper = new QSignalMapper(this);
    int column = 0, row = 0;
    int len = sizeof(imageList) / sizeof(imageList[0]);
    for(int i=0; i<len; ++i)
    {
        ImageInfo info;
        info.author = "360";
        info.name = imageList[i];
        info.path = ":/images/" + imageList[i] + ".jpg";
        // 创建一个缩略图窗口
        SkinItemWidget* item = new SkinItemWidget(info.path, info.name, info.author);
        if(i != 0 && column % 3 == 0)
        {
            column = 0;
            row++;
        }
        info.item = item;
        m_imgInfo.insert(info.path, info);
        gridLayout->addWidget(item, row , column++, 1, 1);

        // 事件映射
        m_mapper->setMapping(item, info.path);
        void (QSignalMapper::*myMap)() = &QSignalMapper::map;
        connect(item, &SkinItemWidget::itemClicked, m_mapper, myMap);
    }
    void (QSignalMapper::*sigMapped)(const QString &) = &QSignalMapper::mapped;
    connect(m_mapper, sigMapped, this, &SkinWindow::imageItemClicked);

    // 设置网格布局属性
    gridLayout->setContentsMargins(0, 0, 0, 0);
    gridLayout->setHorizontalSpacing(0);
    gridLayout->setColumnStretch(0, 1);
    gridLayout->setColumnStretch(1, 2);
    gridLayout->setColumnStretch(2, 3);
    // 创建一个QWidget对象, 将网格布局设置到里边
    QWidget* gridWg = new QWidget;
    gridWg->setLayout(gridLayout);

    // 滚动区域, 并设置属性
    QScrollArea* scrollArea = new QScrollArea;
    scrollArea->setStyleSheet("background-color: rgb(255, 255, 255, 150)");
    scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    scrollArea->setFrameShadow(QFrame::Plain);      //设置平的样式
    scrollArea->setFrameShape(QFrame::NoFrame);     //设置无边样式
    // 网格布局窗口设置到滚动区域中
    scrollArea->setWidget(gridWg);
    scrollArea->setWidgetResizable(true);

    // 垂直布局
    QVBoxLayout* vlayout = new QVBoxLayout;
    vlayout->addWidget(scrollArea);
    vlayout->addLayout(initOpacityWidget());
    vlayout->setContentsMargins(0, 0, 0, 0);
    ui->all_page->setLayout(vlayout);

    // 初始化第一个皮肤为默认选中状态 - 暂时, 用户选中的信息需要存储到配置文件中
    m_curItem = m_imgInfo.first().item;
    m_curItem->setSelected();
}

void SkinWindow::imageItemClicked(const QString &text)
{
    SkinItemWidget* oldItem = m_curItem;
    m_curItem = m_imgInfo[text].item;
    oldItem->setSelected(false);
    m_curItem->setSelected(true);

    // 根据图片设置窗口背景
    MainWindow* pMain = Common::getInstance()->getMainWindow();
    // 主窗口
    pMain->setBkImage(text);
    // 存储当前图片
    Common::getInstance()->setBackgroundImage(text);
    // 刷新当前窗口
    update();
}

QLayout *SkinWindow::initOpacityWidget()
{
    QHBoxLayout* hlayout = new QHBoxLayout;
    QLabel* text = new QLabel("透明度: ");
    QSlider* slider = new QSlider(Qt::Horizontal);
    text->setAttribute(Qt::WA_TranslucentBackground);
    slider->setStyleSheet("background: transparent");
    slider->setRange(0, 255);
    slider->setValue(Common::getInstance()->getMainWinOpacity());
    connect(slider, &QSlider::valueChanged, [=](int value)
    {
        Common::getInstance()->setMainWinOpacity(value);
        Common::getInstance()->getMainWindow()->setBkOpacity(value);
    });

    hlayout->addWidget(text);
    hlayout->addWidget(slider);
    hlayout->addStretch(0);
    hlayout->setContentsMargins(30, 20, 0, 20);
    return hlayout;
}

void SkinWindow::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QString img = Common::getInstance()->getBackgroundImage();
    painter.drawPixmap(10, 10, width()-20, height()-20, QPixmap(img));
}

SkinWindow::~SkinWindow()
{
    delete ui;
}
